home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: cdf.toronto.edu!news
- From: Laventure Derek <a228lave@cdf.toronto.edu>
- Subject: Re: Remove/Insert Function
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <3159B67C.29D6@cdf.toronto.edu>
- Sender: news@cdf.toronto.edu (Usenet News)
- Nntp-Posting-Host: mstlim
- Content-Transfer-Encoding: 7bit
- Organization: Computing Disciplines Facility, University of Toronto
- References: <4j9m80$1cm@freenet-news.carleton.ca>
- Mime-Version: 1.0
- Date: Wed, 27 Mar 1996 21:43:24 GMT
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.3 sun4c)
-
- Hi!
-
- Jerry Boyd wrote:
- >
- > Anyone have suggestions as to how I would WRITE a
- > function (called remove_entry) where the argument
- > would be a pointer into a list. The function
- > would remove the entry after the one that is
- > pointed to by the argument.
-
- Well, I'm not sure how lost you are here, but I'll answer on an
- algorithm level. First, my advice re: anything pointer related:
- pictures. I find that w/o pictures, I can't do much. So, here's what
- you have:
-
- --------- --------- --------- ---------
- | | .|--> | | .|--> | | .|--> | | .|-->etc...
- --------- --------- --------- ---------
- ^ ^Node to be deleted...
- |Node pointed to by argument (*current)
-
- I'll use a basic node type as follows, here.
-
- struct Node{
- int num;
- Node *next;
- } Node;
-
- I'll call the argument pointer current.
-
- So, what needs to be done is:
-
- 1) Save a temp pointer to the node to be deleted (next pointer of the
- node pointed to by current).
-
- 2) Set the next pointer of the node pointed to by current to be the
- next pointer of the node pointed to by temp.
-
- So, the picture changes to the following:
-
- -----------------\
- --------- --------- | --------- ---------
- | | .|--> | | .|-- | | .|--> | | .|-->etc...
- --------- --------- --------- ---------
- ^temp
-
-
- 3) Now, just destroy the memory used by the node pointed to by temp.
-
-
- > Similarly, I need to write a function that would
- > insert a new entry into the link list whereby, the
- > argument is a pointer to the list entry to be
- > inserted.
-
- Insertion is a little trickier, but not too bad... I won't answer here,
- partly because I think I need more details, partly because what I've
- said above may be enough for you. If not, what kind of insert are we
- looking at? I guess you're probably speaking of an ordered list, but
- not sure... Anyway, as I said above, drawing pictures makes a huge
- difference to me... it makes breaking the problem into steps much
- easier. Also, it helps to visualize what's happening, since pointers
- can be somewhat abstract.
-
- Hope this helps... and if not, post back, I'd be happy to try and
- re-explain, or discuss insertion.
-
- Derek
- a228lave@cdf.toronto.edu
-